<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\base\CurrencyModel;
use App\Models\base\ExchangeRateModel;
use App\Http\Requests\finance\ProfitTestRequest;
use Illuminate\Http\Request;

class ProfitTest extends Controller
{
    use ProfitTestRequest;
    
    /**
     * Notes:利润测试
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function total(Request $request) {
        $params = $request->all();
        $time_now = date('Y-m-d H:i:s');

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $get_currency_ids = array_unique(array_column($params['total_get'], 'currency_id'));
        $pay_currency_ids = array_unique(array_column($params['total_pay'], 'currency_id'));

        $get_currency_list = CurrencyModel::whereIn('id', $get_currency_ids)->get();
        $pay_currency_list = CurrencyModel::whereIn('id', $pay_currency_ids)->get();

        $get_total = [];  $pay_total = [];

        foreach ($get_currency_list as $currency) {
            $get_total[$currency->id] = [
                'symbol' => $currency->symbol,
                'amount' => 0
            ];
        }

        foreach ($pay_currency_list as $currency) {
            $pay_total[$currency->id] = [
                'symbol' => $currency->symbol,
                'amount' => 0
            ];
        }

        foreach ($params['total_get'] as $val) {
            foreach ($get_total as $currency_id => $item) {
                if($val['currency_id'] == $currency_id) {
                    $get_total[$currency_id]['amount'] += $val['amount'];
                }else{
                    $time_now = date('Y-m-d H:i:s');
                    $rate = ExchangeRateModel::where('currency_id', $val['currency_id'])
                        ->where('exchange_currency_id', $currency_id)
                        ->where('effect_time', '<', $time_now)
                        ->where('invalid_time', '>=', $time_now)
                        ->value('rate');
                    if(!$rate){
                        return $this->error('汇率设置不完整');
                    }
                    $get_total[$currency_id]['amount'] += round($val['amount'] * $rate, 2);
                }
            }

        }

        foreach ($params['total_pay'] as $val) {
            foreach ($pay_total as $currency_id => $item) {
                if($val['currency_id'] == $currency_id) {
                    $pay_total[$currency_id]['amount'] += $val['amount'];
                }else{
                    $rate = ExchangeRateModel::where('currency_id', $val['currency_id'])
                        ->where('exchange_currency_id', $currency_id)
                        ->where('effect_time', '<', $time_now)
                        ->where('invalid_time', '>=', $time_now)
                        ->value('rate');
                    if(!$rate){
                        return $this->error('汇率设置不完整');
                    }
                    $pay_total[$currency_id]['amount'] += round($val['amount'] * $rate, 2);
                }
            }

        }

        $get_str_arr = [];  $pay_str_arr = [];
        foreach ($get_total as $item) {
            $get_str_arr[] = $item['symbol'] . $item['amount'];
        }

        foreach ($pay_total as $item) {
            $pay_str_arr[] = $item['symbol'] . $item['amount'];
        }

        $get_total_fee = implode(',', $get_str_arr);
        $pay_total_fee = implode(',', $pay_str_arr);


        $get_pay_currency_id = array_intersect($get_currency_ids, $pay_currency_ids);

        $profit_currency_id = $params['profit_currency_id'];
        $profit = '';
        if(in_array($params['profit_currency_id'], $get_pay_currency_id)) {
            $profit_amount = round($get_total[$profit_currency_id]['amount'] - $pay_total[$profit_currency_id]['amount'], 2);
            $profit = $get_total[$profit_currency_id]['symbol'] . $profit_amount;
        }else{
            $get_profit_amount = 0; $pay_profit_amount = 0;
            $profit_symbol = CurrencyModel::where('id', $profit_currency_id)->value('symbol');
            if(in_array($params['profit_currency_id'], $get_currency_ids)) {
                $get_profit_amount = $get_total[$profit_currency_id]['amount'];
            }else{
                if(!empty($get_currency_ids)){
                    $rate = ExchangeRateModel::where('currency_id', $get_currency_ids[0])
                        ->where('exchange_currency_id', $profit_currency_id)
                        ->where('effect_time', '<', $time_now)
                        ->where('invalid_time', '>=', $time_now)
                        ->value('rate');
                    if(!$rate){
                        return $this->error('汇率设置不完整');
                    }
                    $get_profit_amount = round($get_total[$get_currency_ids[0]]['amount'] * $rate, 2);
                }
            }

            if(in_array($params['profit_currency_id'], $pay_currency_ids)) {
                $pay_profit_amount = $pay_total[$profit_currency_id]['amount'];
            }else{
                if(!empty($pay_currency_ids)){
                    $rate = ExchangeRateModel::where('currency_id', $pay_currency_ids[0])
                        ->where('exchange_currency_id', $profit_currency_id)
                        ->where('effect_time', '<', $time_now)
                        ->where('invalid_time', '>=', $time_now)
                        ->value('rate');
                    if(!$rate){
                        return $this->error('汇率设置不完整');
                    }
                    $pay_profit_amount = round($pay_total[$pay_currency_ids[0]]['amount'] * $rate, 2);
                }
            }

            $profit = $profit_symbol . round($get_profit_amount - $pay_profit_amount, 2);
        }

        return $this->success(['get_total_fee' => $get_total_fee, 'pay_total_fee' => $pay_total_fee, 'profit' => $profit]);
    }


}
